<template>
  <div class="app-container">
    <dk-query
      ref="nxIB"
      :searchForm="queryParams"
      :inputList="inputList"
      :queryTable="click().handleQuery"
      labelWidth="110"
    >
    </dk-query>
    <dk-table
      ref="table"
      :column="column"
      :pagination="paginationObjs"
      :tableData="tableData"
      :height="tableHeight"
    >
    </dk-table>
  </div>
</template>

<script>
// 接口api：first step
import { editGoodsBaseType, getGoodsBaseInfo } from '@/api/list/shop-list.js';
import {
  getComAuditList,
  comReSubmit,
  getComDetail,
} from '@/api/merchant-store-manage/submit-record.js';
import { getGoodsTypeList } from '@/api/class/commodity-class.js';

export default {
  data() {
    return {
      loading: true,
      type: 1,
      // 当前这一行的详情数据
      dataInfo: {},
      // 规程toolbar：two step
      inputList: [
        {
          type: 'input',
          placeholder: '请输入',
          label: '商品名称',
          name: 'goodsName',
        },
        {
          type: 'select',
          placeholder: '请输入',
          label: '商品分类',
          name: 'goodsTypeId',
          optionList: [],
        },
        {
          type: 'select',
          placeholder: '请输入',
          label: '状态',
          name: 'auditStatus',
          optionList: [
            // {
            //   name: "待审核",
            //   value: 0,
            // },
            {
              name: '已通过',
              value: 1,
            },
            {
              name: '已驳回',
              value: 2,
            },
          ],
        },
        {
          type: 'input',
          placeholder: '请输入',
          label: '商品ID',
          name: 'goodsId',
        },
        {
          type: 'select',
          placeholder: '请输入',
          label: '是否是促销商品',
          name: 'isPromotion',
          optionList: [
            {
              name: '是',
              value: 0,
            },
            {
              name: '否',
              value: 1,
            },
          ],
        },

        {
          type: 'daterange',
          label: '提交时间',
          name: 'submitTime',
          fun: (row) => {
            this.queryParams.submitTime[0] = `${row[0]} 00:00:00`;
            this.queryParams.submitTime[1] = `${row[1]} 23:59:59`;
          },
        },

        {
          type: 'common-button',
          searchFun: (searchForm) => {
            this.queryParams = searchForm;
            this.click().handleQuery();
          },
          resetFun: () => {
            this.click().resetQuery();
          },
        },
      ],
      // 规程表头：shree step
      column: [
        {
          value: 'goodsId',
          name: '商品ID',
        },
        {
          value: 'goodsName',
          name: '商品名称',
        },
        {
          value: 'sort',
          name: '排序',
        },
        {
          value: 'goodsTypeName',
          name: '商品分类',
        },
        {
          value: 'coverImg',
          name: '封面图',
          type: 'image',
        },
        {
          value: 'inventory',
          name: '库存',
        },
        {
          value: 'platformPrice',
          name: '尊享价',
        },
        {
          value: 'isPromotion',
          name: '是否促销商品',
          stateList: [
            {
              name: '是',
              value: 1,
            },
            {
              name: '否',
              value: 0,
            },
          ],
        },
        {
          value: 'auditStatus',
          name: '状态',
          stateList: [
            {
              name: '待审核',
              value: 0,
            },
            {
              name: '已通过',
              value: 1,
            },
            {
              name: '已驳回',
              value: 2,
            },
          ],
        },
        {
          value: 'createTime',
          name: '提交时间',
        },
        {
          width: 120,
          value: 'id',
          name: '管理',
          fixed: 'right',
          type: 'button',
          buttonList: [
            {
              type: 'no-tooltip',
              name: '重新提交',
              fun: (row) => {
                // this.click().handleResubmit(row);
                this.$router.push({
                  path: `/merchant-store-manage/commodityManage/edit/${row.goodsId}`,
                });
              },
              showFun: (row) => {
                return row.auditStatus === 2;
              },
              color: 'text',
            },
            {
              type: 'no-tooltip',
              name: '详情',
              fun: (row) => {
                this.click().handleDetail(row);
              },
              color: 'text',
              hasPermiss: () => `${this.urlObject.permissPrefix}detail`,
            },
          ],
        },
      ],
      // 分页器
      paginationObjs: {
        paginationChange: function () {
          this.network().getList(false);
        }.bind(this),
        page: 1,
        limit: 10,
        total: 0,
        loading: false,
      },
      // 查询参数
      queryParams: {},
      // 表格数据
      tableData: [],
      // url对象：four step
      urlObject: {
        permissPrefix: 'merchant-store-manage:submit-record:',
        routePathPrefix: '/merchant-store-manage/submitRecord/',
      },
      // 接口存储对象：five step
      interfaceObj: {
        getApiName: getComAuditList,
        groupApiName: getGoodsTypeList,
      },
    };
  },
    props: {
    actName: {
      type: String,
      default: "",
    },
  },
  created() {
    this.network().getList();
    this.getTableHeight.call(this);
    this.network().getGroup();
  },
  methods: {
    click() {
      return {
        // 搜索
        handleQuery: async () => {
          this.paginationObjs.page = 1;
          this.network().getList();
        },
        // 重置
        resetQuery: async () => {
          this.queryParams = this.$options.data().queryParams;
          this.paginationObjs = this.$options.data().paginationObjs;
          this.network().getList();
        },
        // 详情
        handleDetail: async (row) => {
          this.$router.push({
            path: `${this.urlObject.routePathPrefix}detail/${row.id}`,
            query: {
              actName:this.actName,
              bannerType: row.bannerType,
              goodsId:row.goodsId
            },
          });
        },
        // 重新提交
        handleResubmit: async (row) => {
          this.$router.push({
            path: `/merchant-store-manage/commodityManage/edit/${row.goodsId}`,
          });
        },
      };
    },
    network() {
      return {
        getList: async () => {
          const params = this.deepCopy(this.queryParams);
          delete params.dateRange;
          const obj = {
            ...params,
            pageNum: this.paginationObjs.page,
            pageSize: this.paginationObjs.limit,
            beginTime: this.queryParams.submitTime?.[0],
            afterTime: this.queryParams.submitTime?.[1],
            submitTime: undefined,
          };
          this.loading = true;
          try {
            const { getApiName } = this.interfaceObj;
            const data = await getApiName(obj);
            this.loading = false;
            this.tableData = data.rows;
            this.paginationObjs.total = data.total;
          } catch (error) {
            this.loading = false;
          }
        },
        // 查询详情数据
        getInfo: async (id) => {
          const { code, data } = await getComDetail({ id });
          if (code == 0) {
            this.dataInfo = data.auditRecordList.find((item) => (item.id = id));
            console.log(this.dataInfo);
          }
        },
        // 获取分类
        getGroup: async () => {
          const { groupApiName } = this.interfaceObj;
          const { code, rows } = await groupApiName({ type: this.type });
          if (code === 0) {
            const goodsTypeId = this.inputList.find(
              (item) => item.name === 'goodsTypeId'
            );
            goodsTypeId.optionList = rows.map((item, index) => {
              return {
                name: item.typeName,
                value: item.id,
              };
            });
          }

          return rows;
        },
        // handleComfirm: async (row) => {
        //   try {
        //     const { data } = await getGoodsBaseInfo(row.goodsId);
        //     const result = await editGoodsBaseType({
        //       ...data,
        //       createTime: undefined,
        //       updateTime: undefined,
        //       type: this.type,
        //     });
        //     if (result.code == 0) {
        //       this.$message({
        //         message: result.message || '提交成功',
        //         type: 'success',
        //       });
        //     } else {
        //       this.$message({
        //         message: '提交失败',
        //         type: 'error',
        //       });
        //     }
        //     this.network().getList();
        //   } catch (error) {
        //     this.$message({
        //       message: '提交失败',
        //       type: 'error',
        //     });
        //   }
        // },
      };
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-message-box__title span {
  position: relative;
  left: 37px !important;
}

.order-state-row {
  font-size: 14px;
  font-weight: bold;
  color: #606266;
  padding: 0px 40px;
}
::v-deep .cell {
  .el-button {
    margin-right: 5px;
  }
}
</style>
